package zipcodes;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

public class ZipDogSub extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField dong;
	private JComboBox baseAddr = new JComboBox();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ZipDogSub dialog = new ZipDogSub(null, null, null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	// public ZipcodeSub() {
	public ZipDogSub(final JTextField zip1,final JTextField zip2,final JTextField addr) {
		setTitle("우편번호 상세검색");
		setBounds(100, 100, 450, 200);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		JLabel txtDong = new JLabel("읍, 면, 동");
		dong = new JTextField();
		dong.setColumns(10);
		JButton findBtn = new JButton("주소검색");
		findBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (dong.getText().isEmpty()) {
					JOptionPane.showMessageDialog(null, "동을 입력하세요");
				}
				ArrayList<String> zip = ZipDogDAO.findzip( dong.getText() );
				
				// ArrayList에 담긴 내용을 배열로 옮겨 담음
				String[] addr = new String[zip.size()];
				for (int i = 0; i < zip.size(); ++i) {
					addr[i] = zip.get(i);
				}
				baseAddr.removeAllItems(); // 기존 목록 제거
				for (String l : addr) {
					// addr의 내용을 하나 씩 가져와서 String으로 변환 후 하나씩 추가
					baseAddr.addItem(l); // 목록 새로 추가
				}
			}
		});
		
		JButton sendBtn = new JButton("선택후 닫기");
		sendBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String tmp1 = (String) baseAddr.getSelectedItem();
				String zipA = tmp1.split(" ")[0].substring(0, 3);
				String zipB = tmp1.split(" ")[0].substring(3, 6);
				String addrA = tmp1.substring(7);
				
				// ZipcodeMain의 zip1, zip2, addr, etcAddrA에 사용자가 선택하고 입력한 값 전달
				zip1.setText(zipA);
				zip2.setText(zipB);
				addr.setText(addrA);
				// etcAddrA.setText( etcAddr.getText() );
				// 창 닫기
				dispose();
				
				
			}
		});
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(67)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtDong)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(dong, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(findBtn))
						.addComponent(baseAddr, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addContainerGap(85, Short.MAX_VALUE))
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(160)
					.addComponent(sendBtn)
					.addContainerGap(167, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap(75, Short.MAX_VALUE)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtDong)
						.addComponent(dong, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(findBtn))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(baseAddr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(sendBtn)
					.addGap(52))
		);
		contentPanel.setLayout(gl_contentPanel);
	}
}
